package com.zhongwang.business.module.estimateBudgetAccountingManage.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.BusBudgetAccountingCost;
import com.zhongwang.business.gen.entity.BusBudgetAccountingTask;
import com.zhongwang.business.gen.entity.BusDataSource;
import com.zhongwang.business.gen.entity.BusMaterialData;
import com.zhongwang.business.module.estimateBudgetAccountingManage.dao.AccountingInfoDao;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.BusMaterialDataModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.DesignWeightModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.OutModel;
import com.zhongwang.business.util.ShiroUtils;
import com.zhongwang.business.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author 徐一贺
 * @className OutImportListener
 * @description 外协件信息导入监听器
 * @date 2020/11/21 14:46
 * @versron ver 1.0
 */
public class WeightImportListener extends AnalysisEventListener<DesignWeightModel> {
  /** 钩子 */
  private ImportCallBack callBack;
  /** Excel相关参数模型 */
  private BusBudgetAccountingCost param;
  /** 材料信息数据持久层 */
  private AccountingInfoDao accountingInfoDao;
  /** 错误列表 */
  private List<DesignWeightModel> errorList = new ArrayList<>();
  /** 持久化列表 */
  private List<DesignWeightModel> persistenceList = new ArrayList<>();
  /** 当前时间 */
  private Date currentTime = new Date();
  /** 专业部分信息 */
  private Map<String,List<String>> majorParts;
  /**
   * 构造函数
   *
   * @param param 页面参数
   * @param callBack 回调函数
   * @param accountingInfoDao 持久层
   */
  public WeightImportListener(
      BusBudgetAccountingCost param, ImportCallBack callBack, AccountingInfoDao accountingInfoDao) {
    this.callBack = callBack;
    this.param = param;
    this.accountingInfoDao = accountingInfoDao;
    majorParts = new HashMap<>();
    BusBudgetAccountingCost findPartListParam = new BusBudgetAccountingCost();
    findPartListParam.setBatId(param.getBatId());
    findPartListParam.setMajor("机械");
    List<String> mechanicsPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
    findPartListParam.setMajor("流体");
    List<String> fluidPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
    findPartListParam.setMajor("自动化");
    List<String> automationPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
    majorParts.put("机械",mechanicsPart);
    majorParts.put("流体",fluidPart);
    majorParts.put("自动化",automationPart);
  }

  /**
   * 验证
   *
   * @param data 每行数据
   */
  @Override
  public void invoke(DesignWeightModel data, AnalysisContext context) {
    String errorInfo = "";
    boolean isRightMajor = false;
    if(data.getMajor()==null || StringUtils.trimAllBlank(data.getMajor()).equals("")){
      errorInfo = "请填写专业！\r\n";
    }else if(data.getMajor()!= null && data.getMajor().length()>255){
      errorInfo = "专业填写不正确，正确的是机械、流体或自动化！\r\n";
    }
    if(data.getMajor().equals("机械")||data.getMajor().equals("流体") || data.getMajor().equals("自动化")){
      isRightMajor=true;
    }else {
      errorInfo = "专业填写不正确，正确的是机械、流体或自动化！\r\n";
    }
    if(data.getPart()==null || StringUtils.trimAllBlank(data.getPart()).equals("")){
      errorInfo = "请填写部分！\r\n";
    }else if(data.getPart()!= null && data.getMajor()!= null && isRightMajor){
      List<String> parts = majorParts.get(data.getMajor());
      if(!parts.contains(data.getPart())){
        errorInfo = "部分填写错误，该专业下无此部分！\r\n";
      }
    }

    if (data.getLaborName() == null || StringUtils.trimAllBlank(data.getLaborName()).equals("")) {
      errorInfo = "请填写重量类别名称！\r\n";
    }else if (!data.getLaborName().equals("结构总重")
            && !data.getLaborName().equals("结构净重-碳钢")
            && !data.getLaborName().equals("结构净重-不锈钢")
            && !data.getLaborName().equals("结构净重-铝材")
            && !data.getLaborName().equals("结构净重-其他")) {
      errorInfo = "重量类别名称只能填写结构总重或者结构净重-碳钢或者结构净重-不锈钢或者结构净重-铝材或者结构净重-其他！\r\n";
    }
    if (data.getLaborNum() == null) {
      errorInfo = "请填写数量！\r\n";
    }
    if (data.getUnit() == null || StringUtils.trimAllBlank(data.getUnit()).equals("")) {
      errorInfo = "请填写单位，并且单位只能是吨！\r\n";
    }else if (!data.getUnit().equals("吨")) {
      errorInfo = "单位只能填写吨！\r\n";
    }
    if (errorInfo.length() > 0) {
      data.setErrorInfo(errorInfo);
      errorList.add(data);
    } else {
      persistenceList.add(data);
    }
  }

  /**
   * 持久化
   *
   * @param context
   */
  @Override
  public void doAfterAllAnalysed(AnalysisContext context) {
    if(errorList.size()>0){
      callBack.onWarning(errorList);
    }else{
      BusBudgetAccountingTask order = accountingInfoDao.getBusBudgetAccountingTaskModelById(param.getBatId());
      for (int i = 0; i < persistenceList.size(); i++) {
        DesignWeightModel persistenceData = persistenceList.get(i);
        persistenceData.setBatId(param.getBatId());
        if (persistenceData.getMajor().equals("机械")) {
          persistenceData.setType(1);
        } else if (persistenceData.getMajor().equals("流体")) {
          persistenceData.setType(2);
        } else if (persistenceData.getMajor().equals("自动化")) {
          persistenceData.setType(3);
        }
        if (persistenceData.getLaborName().equals("结构总重")) {
          persistenceData.setLaborType(3);
        } else if (persistenceData.getLaborName().equals("结构净重-碳钢")
                || persistenceData.getLaborName().equals("结构净重-不锈钢")
                || persistenceData.getLaborName().equals("结构净重-铝材")
                || persistenceData.getLaborName().equals("结构净重-其他")) {
          persistenceData.setLaborType(2);
        }
        BusBudgetAccountingCost getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
        getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
        getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
        getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
        getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor()+"-"+persistenceData.getPart()+"-设计费");
        BusBudgetAccountingCost setCostModel = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        setCostModel.setDataSource("1");
        accountingInfoDao.updateBusBudgetAccountingCostDataSourceByPrimaryKey(setCostModel);
        persistenceData.setCostId(setCostModel.getId());
        persistenceData.setProCode(order.getProCode());
        persistenceData.setDataUseType(2);
        persistenceData.setGetDate(currentTime);
        persistenceData.setGetUid(ShiroUtils.getUserId());
        DesignWeightModel existData = accountingInfoDao.getImportWeightExistDataByCondition(persistenceData);
        if(existData!=null){
          accountingInfoDao.deleteDesignWeightModelByPrimary(existData.getId());
        }
        accountingInfoDao.insertWeightImport(persistenceData);
      }
      callBack.onSuccess();
    }
  }

  /**
   * 异常处理
   *
   * @param exception 异常信息
   * @param context
   */
  @Override
  public void onException(Exception exception, AnalysisContext context) {

  }
}
